package com.example.prom.ok.y2023.work.emailFooter;

import com.alibaba.fastjson.JSON;
import com.example.prom.listener.ExcelListener;
import com.example.prom.ok.y2023.work.maxmind.MaxMindIpReaderVO2;
import com.example.prom.ok.y2023.work.maxmind.RegionVO2;
import com.example.prom.strategy.excel.CustomCellStyleStrategy;
import com.example.prom.strategy.excel.CustomColumnWidthStrategy;
import com.fasterxml.jackson.databind.deser.impl.BeanPropertyMap;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static com.alibaba.excel.EasyExcelFactory.read;
import static com.alibaba.excel.EasyExcelFactory.write;

public class EmailFooterHandler {

    public static void main(String[] args) {
        List<EmailFooterWriteVO> writeVOList = new ArrayList<>();
        List<EmailFooterHandlerVO> emailFooterHandlerVOS = readExcel();
        for (EmailFooterHandlerVO emailFooterHandlerVO : emailFooterHandlerVOS) {
            String footer = getFooter();
            String language = emailFooterHandlerVO.getLanguage();
            String grating = emailFooterHandlerVO.getGrating();
            String contactUs = emailFooterHandlerVO.getContactUs();
            String legalDesc = emailFooterHandlerVO.getLegalDesc();
            String copyright = emailFooterHandlerVO.getCopyright();
            String[] contactUsSplit = contactUs.split("[?？]");
            if (contactUsSplit.length<2) {
                // todo 泰国语没有问号需要特殊处理
                contactUsSplit = contactUs.split(" ");
                footer = footer.replace("?","");
//                System.out.println("indiaFooter:"+indiaFooter);
//                continue;
            }
            System.out.println(JSON.toJSONString(contactUsSplit));
            String contact = contactUsSplit[1];
            contact = contact.substring(0, contact.length() - 1);

            EmailFooterWriteVO writeVO = new EmailFooterWriteVO();
            footer = footer.replace("Thank you for choosing OKX.",grating)
                    .replace("Need help",contactUsSplit[0])
                    .replace("Contact our customer support",contact)
                    .replace("This email is provided for informational purposes only.",legalDesc)
                    .replace("Copyright", copyright);

            int i = language.lastIndexOf("（");
            if (i < 0) {
                i = language.lastIndexOf("(");
                System.out.println(language);
            }
            String lang = language.substring(i + 1, language.length() - 1);
            if (lang.equalsIgnoreCase("zh_CN") || lang.equalsIgnoreCase("zh_HK")){
                footer = footer.replace("请联系客服</a>.","请联系客服</a>。" )
                .replace("?","？");
            }
            System.out.println(lang);
            writeVO.setLanguage(lang);
            writeVO.setContent(footer);
            writeVOList.add(writeVO);


        }

        writeExcel(writeVOList);
    }



    public static List<EmailFooterHandlerVO> readExcel() {
        String fileName = "C:\\Users\\hp\\Desktop\\ok\\data\\emailFooterSource.xlsx";
//        String fileName = "C:\\Users\\hp\\Desktop\\ok\\work\\gatewayBannedIps.xlsx";
        ExcelListener<EmailFooterHandlerVO> excelListener = new ExcelListener<>();
        read(fileName, EmailFooterHandlerVO.class, excelListener).sheet().doRead();
        List<EmailFooterHandlerVO> dataList = excelListener.getDataList();
        return dataList;
    }

    public static void writeExcel(List<EmailFooterWriteVO> writerList){
        String writeFileName = "C:\\Users\\hp\\Desktop\\ok\\data\\emailFooterDest5.xlsx";
        write(writeFileName, EmailFooterWriteVO.class).sheet().head(EmailFooterWriteVO.class)
                .registerWriteHandler(new CustomColumnWidthStrategy())
                .registerWriteHandler(new CustomCellStyleStrategy()).doWrite(writerList);
    }


    public static String getFooter(){
        return "<table align=\"center\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" role=\"presentation\" width=\"100%\"> <tbody> <tr> <td> <div style=\"width: 100%; max-width: 600px; direction: ltr; font-size: 0px; padding: 40px 24px 40px 24px; font-family: HarmonyOS Sans, SF Pro Text, SF Pro Icons, robot, Helvetica Neue, Helvetica, Arial, sans-serif; background-color: #000000; box-sizing: border-box; margin: 70px auto 0;\"> <table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" role=\"presentation\" width=\"100%\" style=\"font-family: HarmonyOS Sans, SF Pro Text, SF Pro Icons, robot, Helvetica Neue, Helvetica, Arial, sans-serif; background-color: #000000\"> <tbody> <tr> <td> <table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" role=\"presentation\" width=\"100%\" style=\"max-width: 348px; margin: auto\"> <tbody> <tr> <td style=\"font-size:0px;word-break:break-word; height: 28px;\"> <a style=\"text-decoration: none;\" target=\"_blank\" rel=\"noopener noreferrer\" href=\"${THEMETWITTER}\" data-hs-link-id=\"0\"> <img src=\"https://static.coinall.ltd/cdn/assets/imgs/239/2F97507FBD4420FF.png\" width=\"36\" height=\"36\" alt=\"twitter\" style=\"width: 36px; height: 36px\"> </a> </td> <td width=\"10%\"></td> <td style=\"font-size:0px;word-break:break-word; height: 28px;\"> <a style=\"text-decoration: none;\" target=\"_blank\" rel=\"noopener noreferrer\" href=\"${THEMETELEGRAM}\" data-hs-link-id=\"0\"> <img src=\"https://static.coinall.ltd/cdn/assets/imgs/238/3A1A2907636E422B.png\" width=\"36\" height=\"36\" alt=\"telegram\" style=\"width: 36px; height: 36px\"> </a> </td> <td width=\"10%\"></td> <td style=\"font-size:0px;word-break:break-word; height: 28px;\"> <a style=\"text-decoration: none;\" target=\"_blank\" rel=\"noopener noreferrer\" href=\"${THEMETIKTOK}\" data-hs-link-id=\"0\"> <img src=\"https://static.coinall.ltd/cdn/assets/imgs/238/951D1A29C9A22BD2.png\" width=\"36\" height=\"36\" alt=\"tiktok\" style=\"width: 36px; height: 36px\"> </a> </td> <td width=\"10%\"></td> <td style=\"font-size:0px;word-break:break-word; height: 28px;\"> <a style=\"text-decoration: none;\" target=\"_blank\" rel=\"noopener noreferrer\" href=\"${THEMELINKEDIN}\" data-hs-link-id=\"0\"> <img src=\"https://static.coinall.ltd/cdn/assets/imgs/238/F0749A0D24C6643D.png\" width=\"36\" height=\"36\" alt=\"linkedin\" style=\"width: 36px; height: 36px\"> </a> </td> <td width=\"10%\"></td> <td style=\"font-size:0px;word-break:break-word; height: 28px;\"> <a style=\"text-decoration: none;\" target=\"_blank\" rel=\"noopener noreferrer\" href=\"${THEMEFACEBOOK}\" data-hs-link-id=\"0\"> <img src=\"https://static.coinall.ltd/cdn/assets/imgs/238/5E1CBF0956D813EF.png\" width=\"36\" height=\"36\" alt=\"facebook\" style=\"width: 36px; height: 36px\"> </a> </td> <td width=\"10%\"></td> <td style=\"font-size:0px;word-break:break-word; height: 28px;\"> <a style=\"text-decoration: none;\" target=\"_blank\" rel=\"noopener noreferrer\" href=\"${THEMEREDDIT}\" data-hs-link-id=\"0\"> <img src=\"https://static.coinall.ltd/cdn/assets/imgs/238/23394D053A2F63FD.png\" width=\"36\" height=\"36\" alt=\"reddit\" style=\"width: 36px; height: 36px\"> </a> </td> </tr> </tbody> </table> </td> </tr> <tr> <td align=\"center\" style=\"font-size:0px; padding-top:24px; text-align: left;\"> <div style=\"max-width: 500px;margin: auto;\"> <div style=\"margin-bottom: 12px;\"> <span style=\"font-weight: 400; font-size: 12px; line-height: 16px; color: #CCCCCC\"> Thank you for choosing OKX. </span> </div> <span style=\"font-weight: 400; font-size: 12px; line-height: 16px; color: #CCCCCC\"> Need help? <a href=\"https://www.okx.com/support-center.html\" style=\"color:#FFFFFF;\" data-hs-link-id=\"0\" target=\"_blank\"> Contact our customer support</a>. </span> </div> </td> </tr> <tr> <td align=\"center\" style=\"padding-top:24px;\"> </td> </tr> <tr> <td> <div style=\"border-top: 1px solid #3F3F3F; max-width: 500px; margin: auto; font-weight: 400; text-align: left; font-size: 12px; line-height: 18px\"> <span style=\"color: #959595; margin-top: 12px; display: block\"> This email is provided for informational purposes only. </span> </div> </td> </tr> <tr> <td align=\"center\" style=\"font-size:0px;padding-top:24px;text-align: center\"> <span style=\" font-size:12px; line-height:16px; color:#CCCCCC;\"> Copyright </span> </td> </tr> </tbody> </table> </div> </td> </tr> </tbody> </table>";
    }
}
